package middlewares

import (
	"api/go-demo/config"
	"api/go-demo/utils"
	"fmt"
	"time"

	"github.com/gin-gonic/gin"
)

func Config(tomlConfig *config.Config) gin.HandlerFunc {
	return func(c *gin.Context) {
		c.Set("config", tomlConfig)
		c.Next()
	}
}

//针对用户web请求拦截器
func LogRequest(tomlConfig *config.Config) gin.HandlerFunc {
	//生存日志的信息，包括消息通知中心，服务名称，requestId
	return func(c *gin.Context) {
		userInfo := c.MustGet("token").(*utils.Claims)
		log := &utils.LogInfo{}
		requestID := fmt.Sprintf("%d#%d", userInfo.UserID, time.Now().Unix())
		log.RequestID = requestID
		c.Set("logInfo", log)
		c.Next()
	}
}

//针对内部服务请求拦截器
func LogRequestApi(tomlConfig *config.Config) gin.HandlerFunc {
	return func(c *gin.Context) {
		log := &utils.LogInfo{}
		requestID := fmt.Sprintf("%s#%d", "httpapi", time.Now().Unix())
		log.RequestID = requestID
		c.Set("logInfo", log)
		c.Next()
	}
}
